﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EnemyManager : MonoBehaviour 
{
    private float playerDistance;
    public PlayerManager playerManager;
    public int Max_Enemies_level1 = 1;
    public int Minimum_Distance_Enemies_level1 = 100;
    public List<EnemyBase> enemies_level1;

    private int enemies_Level1_Counter;

    private bool createEnemyLevel1;
    

	// Use this for initialization
	void Start ()
    {
        playerDistance = 0;
        enemies_Level1_Counter = 0;
        createEnemyLevel1 = false;
        checkForInitErrors();
	}
	
	// Update is called once per frame
	void Update () 
    {
        playerDistance = playerManager.getDistance();

        if (playerDistance > Minimum_Distance_Enemies_level1)
        {
            createEnemyLevel1 = true;
        }

        if (createEnemyLevel1 == true)
        {
            generateEnemyLevel1();
        }
	}

    private void generateEnemyLevel1()
    {
        
        if (enemies_Level1_Counter < Max_Enemies_level1)
        {
            createEnemyLevel1 = false;
            enemies_Level1_Counter++;
            EnemyBase enemy = (EnemyBase)Instantiate(enemies_level1[0], enemies_level1[0].transform.position, Quaternion.identity);
            string scriptName = enemy.name.Replace("(Clone)", "") + "Manager";
            (enemy.GetComponent(scriptName) as EnemyBase).Init();
           // (enemy.GetComponent(scriptName) as EnemyBase).setIsAlive(true);
           // Debug.Log("scriptName = " +  + Global.DEBUG_TEXT_ARROW);
            
        }
    }

    private void checkForInitErrors()
    {
        if (enemies_level1.Count == 0)
        {
            Debug.Log("EnemyManager: Missing Enemies" + Global.DEBUG_TEXT_ARROW);
        }

        if (playerManager == null)
        {
            Debug.Log("EnemyManager: Missing playerManager" + Global.DEBUG_TEXT_ARROW);
        }
    }

}
